//
//  PublishCommentView.m
//  ChildHoodStemp
//
//  Created by zhuxuehang on 13-10-31.
//
//
#import <QuartzCore/QuartzCore.h>
#import "PublishCommentView.h"
//#import "CHPostService.h"
//#import "ChildHoodConsts.h"
//#import "ChildHoodPost.h"
#import "MBProgressHUD.h"
#import "MicroDef.h"


@interface PublishCommentView ()<UITextViewDelegate>
{
    int64_t _replyPostID;
    id _server;
}
@end

@implementation PublishCommentView
@synthesize commentTextView = _commentTextView;

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

-(id)initWithReplyPostId:(int64_t)replyPostID server:(id)server
{
    if (self = [super init]) {
        _replyPostID = replyPostID;
        _server = server;
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"完成" style:UIBarButtonItemStyleDone target:self action:@selector(publishComment:)];

    _commentTextView = [[UITextView alloc] initWithFrame:CGRectMake(10, 10, HARDWARE_SCREEN_WIDTH - 20 , 200)];
    [_commentTextView setDelegate:self];
    [_commentTextView setAutoresizingMask:UIViewAutoresizingFlexibleHeight];
    [_commentTextView setReturnKeyType:UIReturnKeyDefault];
    [_commentTextView setShowsVerticalScrollIndicator:NO];
    [_commentTextView setKeyboardType:UIKeyboardTypeDefault];
    [_commentTextView setTextColor:[UIColor darkGrayColor]];
    [_commentTextView.layer setCornerRadius:8];
    [_commentTextView setClipsToBounds:YES];
    [_commentTextView setFont:[UIFont systemFontOfSize:12]];
    [self.view addSubview:_commentTextView];
    
//    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(onPublishComment:) name:NOTIFY_MSG_MYPOSTS object:nil];
    
    [self.view setUserInteractionEnabled:YES];
    [self.view addGestureRecognizer:[[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(keyboardDismiss)]];
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

-(void)dealloc
{
    NSLog(@"PublishCommentView dealloc");
    [[NSNotificationCenter defaultCenter] removeObserver:self];
}

-(void)publishComment:(id)sender
{
    [_commentTextView resignFirstResponder];
    if (![_commentTextView.text isEqualToString:@""]) {
        //发表内容
//        [_server addMyPostCommentWithContent:_commentTextView.text replyPostID:_replyPostID];
//        
//        MBProgressHUD* progress = [[MBProgressHUD alloc] initWithView:self.view];
//        [progress setAlpha:0.5];
//        progress.removeFromSuperViewOnHide = YES;
//        [progress setLabelText:@"正在发送"];
//        [self.view addSubview:progress];
//        [self.view bringSubviewToFront:progress];
//        [progress show:YES];
    }else
    {
        UIAlertView *alert = [[UIAlertView alloc] initWithTitle:nil message:@"发表内容不能为空" delegate:self cancelButtonTitle:@"取消" otherButtonTitles: nil];
        [alert show];
    }
}

-(void)onPublishComment:(NSNotification*)noti
{
//    ChildHoodPost* post = noti.object;
//    if (post.postSta == kPostSendOK) {
//        [UIView animateWithDuration:0.3 animations:^{
//            MBProgressHUD* progress = [[MBProgressHUD alloc] initWithView:self.view];
//            [progress setAlpha:0.5];
//            progress.removeFromSuperViewOnHide = YES;
//            [progress setLabelText:@"发送成功"];
//            [self.view addSubview:progress];
//            [self.view bringSubviewToFront:progress];
//            [progress show:YES];
//        } completion:^(BOOL finished) {
//            [MBProgressHUD hideAllHUDsForView:self.view animated:YES];
//        }];
//        [self.navigationController popViewControllerAnimated:YES];
//    }
}

-(void)keyboardDismiss
{
    [_commentTextView resignFirstResponder];
}
@end
